import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.Vector;

public class Macros {

	public static void DEBUG(String debugString) {

		System.out.println(GET_TIME_STRING() + debugString);
	}

	public static String GET_TIME_STRING() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = new Date();
		return dateFormat.format(now) + ": ";
	}

	public static boolean getDevice(byte deviceID, long devices) {
		if (deviceID > 64) {
			return false;
		}

		long result;
		long bitMask = 1;

		result = devices & (bitMask << (deviceID - 1));
		
		return (result > 0);
	}

	public static long setDevice(byte deviceID, long devices) {
		if (deviceID > 64) {
			return devices;
		}
		
		long bitMask = 1;
		
		devices = devices | (bitMask << (deviceID - 1));

		return devices;
	}

	public static long resetDevice(byte deviceID, long devices) {
		if (deviceID > 64) {
			return devices;
		}
		
		long bitMask = 1;

		devices = devices & ~(bitMask << (deviceID - 1));

		return devices;
	}

	public static String getDeviceString(long devices) {

		String deviceString = "Devices: ";
		long bitMask = 1;

		for (int i = 0; i < 64; i++) {

			if ((devices & (bitMask << i)) > 0) {
				deviceString = deviceString + (i + 1) + ", ";
			}
		}

		return deviceString;
	}

	public static boolean getWeekday(int weekday, byte weekdays) {
		byte result;

		switch (weekday) {
		case Calendar.SUNDAY:
			result = (byte) (weekdays & 0b00000001);
			break;
		case Calendar.MONDAY:
			result = (byte) (weekdays & 0b00000010);
			break;
		case Calendar.TUESDAY:
			result = (byte) (weekdays & 0b00000100);
			break;
		case Calendar.WEDNESDAY:
			result = (byte) (weekdays & 0b00001000);
			break;
		case Calendar.THURSDAY:
			result = (byte) (weekdays & 0b00010000);
			break;
		case Calendar.FRIDAY:
			result = (byte) (weekdays & 0b00100000);
			break;
		case Calendar.SATURDAY:
			result = (byte) (weekdays & 0b01000000);
			break;
		default:
			result = 0;
			break;
		}

		return (result > 0);
	}

	public static byte setWeekday(int weekday, byte weekdays) {

		switch (weekday) {
		case Calendar.SUNDAY:
			weekdays = (byte) (weekdays | 0b00000001);
			break;
		case Calendar.MONDAY:
			weekdays = (byte) (weekdays | 0b00000010);
			break;
		case Calendar.TUESDAY:
			weekdays = (byte) (weekdays | 0b00000100);
			break;
		case Calendar.WEDNESDAY:
			weekdays = (byte) (weekdays | 0b00001000);
			break;
		case Calendar.THURSDAY:
			weekdays = (byte) (weekdays | 0b00010000);
			break;
		case Calendar.FRIDAY:
			weekdays = (byte) (weekdays | 0b00100000);
			break;
		case Calendar.SATURDAY:
			weekdays = (byte) (weekdays | 0b01000000);
			break;
		default:
			break;
		}

		return weekdays;
	}

	public static byte resetWeekday(int weekday, byte weekdays) {

		switch (weekday) {
		case Calendar.SUNDAY:
			weekdays = (byte) (weekdays & ~0b00000001);
			break;
		case Calendar.MONDAY:
			weekdays = (byte) (weekdays & ~0b00000010);
			break;
		case Calendar.TUESDAY:
			weekdays = (byte) (weekdays & ~0b00000100);
			break;
		case Calendar.WEDNESDAY:
			weekdays = (byte) (weekdays & ~0b00001000);
			break;
		case Calendar.THURSDAY:
			weekdays = (byte) (weekdays & ~0b00010000);
			break;
		case Calendar.FRIDAY:
			weekdays = (byte) (weekdays & ~0b00100000);
			break;
		case Calendar.SATURDAY:
			weekdays = (byte) (weekdays & ~0b01000000);
			break;
		default:
			break;
		}

		return weekdays;
	}

	public static String getDeviceName(Vector<Device> devices, int deviceID) {

		for (int i = 0; i < devices.size(); i++) {
			if (devices.get(i).getDeviceID() == deviceID) {
				return devices.get(i).getDeviceName();
			}
		}

		return "";
	}
}
